package com.jrx.anytxn.customer.controller;

import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.customer.dto.rights.CmCustomerRightsRes;
import com.jrx.anytxn.customer.dto.rights.CustomerRightResByCms;
import com.jrx.anytxn.customer.service.ICustomerRightService;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * 页面相关客户权益查询
 */
@Api(tags = "页面相关的客户相关权益信息")
@RestController
@RequestMapping(value = "/customer/customerRight")
public class CustRightCmsController {

    private static final Logger logger = LoggerFactory.getLogger(CustRightCmsController.class);

    @Autowired
    private ICustomerRightService customerRightService;
    @Autowired
    private InfoService infoService;

    @ApiOperation(value = "界面客户权益查询", notes = "界面客户权益查询")
    @GetMapping("/getCustomerRight/{loanId}/{customerId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "loanId", value = "订单编号", required = true),
            @ApiImplicitParam(name = "customerId", value = "客户编号", required = true)
    })
    public TxnRespResult<ArrayList<CustomerRightResByCms>> getCustomerRight(@PathVariable String loanId, @PathVariable String customerId) throws TxnBizException {
        TxnRespResult<ArrayList<CustomerRightResByCms>> result = new TxnRespResult<>();
        logger.info("界面客户权益查询开始:startTime:{}",result.getStartTime());
        String tenantId=String.valueOf(infoService.getUser().getProjectId());
        ArrayList<CustomerRightResByCms> list = new ArrayList<>();
        CustomerRightResByCms customerRightResByCms = customerRightService.getCustomerRightByLoanIdAndCustomerId(loanId,customerId,tenantId);
        list.add(customerRightResByCms);
        result.getSuccess(list);
        logger.debug("界面客户权益查询结束endTime{}",result.getEndTime());
        return result;
    }

    @ApiOperation(value = "客户权益分页查询", notes = "客户权益分页查询")
    @GetMapping(value = "/getRightsWithPage")
    public TxnRespResult<TxnPage> getRightsWithPage(@RequestParam String channel, @RequestParam String customerId, @RequestParam Integer pageNum,
                                                    @RequestParam Integer pageSize, @RequestParam String seqNo) {
        TxnRespResult<TxnPage> result = new TxnRespResult();
        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        try {
            result = result.getSuccess(customerRightService.getRightsWithPage(channel, customerId, tenantId, pageNum, pageSize));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = result.getFail(TxnRespCode.BIZ_ERROR.getCode(), TxnRespCode.BIZ_ERROR.getUserTips());
        }
        return result;
    }

    @ApiOperation(value = "根据权益ID查询", notes = "根据权益ID查询")
    @GetMapping("/getCustomerRightById/{id}/{customerId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "权益单号", required = true),
            @ApiImplicitParam(name = "customerId", value = "客户编号", required = true)
    })
    public TxnRespResult<CmCustomerRightsRes> getCustomerRightById(@PathVariable Long id, @PathVariable String customerId) throws TxnBizException {
        TxnRespResult<CmCustomerRightsRes> result = new TxnRespResult<>();
        logger.info("根据权益ID查询开始:startTime:{}",result.getStartTime());
        String tenantId=String.valueOf(infoService.getUser().getProjectId());
        result.getSuccess(customerRightService.getCustomerRightByIdAndCustomerId(id,customerId,tenantId));
        logger.debug("根据权益ID查询结束endTime{}",result.getEndTime());
        return result;
    }

}
